####################################################################################
###
###   Immigration, Public Housing and Support for the French National Front
###   Gloria Gennaro
###
###   Paper Figure A30
###
####################################################################################


library(fixest)
library(did)


################################################################################
# Set Up
################################################################################

# Load Working Directories
source(paste0(wd_main, '/2_code/00_working_directories.R'))

# Load data and clean
source(paste0(wd_code, '/05_data_load_clean_absun.R'))

################################################################################
# Analysis
################################################################################

df = df_did

# Parameters

df$treat = df$groupT
df$time_to_treat = df$policy_period_ind
df$year_treated = df$sru_period
df$year_treated = ifelse(df$treat==0, 10000, df$year_treated )


################################################################################
# Estimation
################################################################################


temp = df[df$imm_quant_99==1,]
mod_twfe1 = feols(fn ~ i(time_to_treat, treat, ref = -1) + running | CODGEO + policy_period,              
                 cluster = ~CODGEO, data = temp)
mod_sa1 = feols(fn ~ sunab(year_treated, policy_period) +  running |  CODGEO + policy_period,         
               cluster = ~CODGEO,  data = temp)


temp = df[df$imm_quant_99==2,]
mod_twfe2 = feols(fn ~ i(time_to_treat, treat, ref = -1) + running | CODGEO + policy_period,            
                  cluster = ~CODGEO, data = temp)
mod_sa2 = feols(fn ~ sunab(year_treated, policy_period) +  running |  CODGEO + policy_period,         
                cluster = ~CODGEO,  data = temp)


temp = df[df$imm_quant_99==3,]
mod_twfe3 = feols(fn ~ i(time_to_treat, treat, ref = -1) + running | CODGEO + policy_period,              
                  cluster = ~CODGEO, data = temp)
mod_sa3 = feols(fn ~ sunab(year_treated, policy_period) +  running |  CODGEO + policy_period,         
                cluster = ~CODGEO,  data = temp)



#################################################################################
# Plot
################################################################################

pdf(file=paste0(wd_res, '/figures/figA30.pdf'), height=7, width=10)

iplot(list(mod_sa1, mod_sa2, mod_sa3),
  col = c(2, 3, 4),
  sep = 0.1, ref.line = -1,
  xlab = 'Period relative to policy implementation',
  ylab = 'NF Vote Share (percentage points)',
  main = 'Sun and Abraham (2021) estimator')

legend("bottomleft", col = c(2, 3, 4), pch = c(20, 17, 15), 
       legend = c("Low Immigration",
                  "Medium Immigration",
                  "High Immigration"))

dev.off()
